package cn.oms.manage.service.impl;

import cn.oms.commons.ex.ServiceException;
import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.util.StringToDate;
import cn.oms.commons.web.ServiceCode;
import cn.oms.manage.dao.persist.repository.IDoctorRepository;
import cn.oms.manage.pojo.entity.Doctor;
import cn.oms.manage.pojo.entity.Medicine;
import cn.oms.manage.pojo.param.DoctorAddNewParam;
import cn.oms.manage.pojo.param.DoctorUpdateParam;
import cn.oms.manage.pojo.vo.*;
import cn.oms.manage.service.IDoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service
public class DoctorServiceImpl implements IDoctorService {
    @Value("${oms.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IDoctorRepository doctorRepository;

    public DoctorServiceImpl() {
        log.debug("创建业务类对象：DoctorServiceImpl");
    }


    public void addNew(DoctorAddNewParam doctorAddNewParam) {
        log.debug("开始处理【添加医生信息】的业务，参数：{}", doctorAddNewParam);
        Doctor doctor = new Doctor();
        doctor.setBirth(StringToDate.convert(doctorAddNewParam.getBirth()));
        BeanUtils.copyProperties(doctorAddNewParam, doctor);


        log.debug("即将执行插入数据，参数：{}", doctor);
        int rows = doctorRepository.insert(doctor);
        if (rows != 1) {
            String message = "添加医生失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    public void delete(Long id) {
        log.debug("开始处理【根据ID删除医生】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        DoctorStandardVO medicine = doctorRepository.getStandardById(id);
        log.debug("根据ID={}检查医生数据是否存在，查询结果：{}", id, medicine);
        // 判断查询结果是否为null
        if (medicine == null) {
            String message = "删除医生失败，尝试删除的医生数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        doctorRepository.deleteById(id);

    }

    @Override
    public void updateInfoById(Long id, DoctorUpdateParam doctorUpdateParam) {
        log.debug("开始处理【修改药品详情】的业务，ID：{}，新数据：{}", id, doctorUpdateParam);
        // 调用Mapper对象的getStandardById()执行查询
        DoctorStandardVO queryResult = doctorRepository.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改药品详情失败，尝试修改的药品数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 创建医生对象
        Doctor doctor = new Doctor();
        // 复制属性，设置ID
        doctor.setBirth(StringToDate.convert(doctorUpdateParam.getBirth()));
        BeanUtils.copyProperties(doctorUpdateParam, doctor);
        doctor.setId(id);
        // 调用Mapper对象的update()方法执行修改
        int rows = doctorRepository.update(doctor);
        if (rows != 1) {
            String message = "修改药品详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public DoctorStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询药品详情】的业务，参数：{}", id);
        DoctorStandardVO queryResult = doctorRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询药品详情失败，药品数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<DoctorListVO> list(Integer pageNum) {
        log.debug("开始处理【查询药品列表】的业务，页码：{}", pageNum);
        PageData<DoctorListVO> pageData = doctorRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<DoctorListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询药品列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<DoctorListVO> pageData = doctorRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public List<DoctorNameListVO> lisDoctorName() {
        return doctorRepository.listDoctorName();
    }
}
